/**
 * 
 */
package es.xpt.ysf.commons.track;

import static javax.ejb.TransactionAttributeType.REQUIRED;

import java.util.List;

import javax.annotation.Resource;
import javax.ejb.Asynchronous;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;

import es.xpt.ysf.YSFException;
import es.xpt.ysf.track.Track;
import es.xpt.ysf.track.TrackDAO;
import es.xpt.ysf.track.TrackDAOFactory;
import es.xpt.ysf.track.TrackSearchParams;


@Stateless(name = "TrackSession")
@TransactionAttribute(REQUIRED)
public class TrackSessionBean implements TrackSession { // NO_UCD
	
	@Resource private SessionContext context;


	@Override
	public List<Track> searchItem(TrackSearchParams searchParams) throws YSFException {
		if (searchParams == null)
			 throw new NullPointerException("searchParams");
		
		TrackDAOFactory f = null;
	    try {
	         f = new TrackDAOFactory(context.getCallerPrincipal());
	         TrackDAO dao = f.getTrackDAO();	         
	         return dao.searchTrack(searchParams);
	     } finally {
	         if (f!=null) 
	        	 f.release();
	     }
	}
	
	@Override
	@Asynchronous
	public void insertTrack(Track newTrack) throws YSFException {
		TrackDAOFactory f = null;
	    try {
	         f = new TrackDAOFactory(context.getCallerPrincipal());
	         f.getTrackDAO().insertTrack(newTrack);
	     } finally {
	         if (f!=null) 
	        	 f.release();
	     }
	}

}
